package com.turtle3d.formallanguage.symbol;

import java.util.ArrayList;
import java.util.Hashtable;

/**
 * L-System symbol, LSymbolClass instance.
 * 
 * @author Marek Paterczyk <marek.paterczyk@gmail.com>
 *
 */
public class Symbol {
	private String symbol;
	private SymbolClass symbolClass;
		
	private Hashtable<String, Float> parameters = new Hashtable<String, Float>();

	public Symbol(String symbol) {
		super();
		this.symbol = symbol;
	}		
	
	public Symbol(SymbolClass symbolClass)
	{
		super();
		this.symbolClass = symbolClass;
		symbol = symbolClass.getSymbol();
	}

	public String toString(){
		if(parameters.isEmpty())
			return symbol;
		
		String vars="";
		for(String key:parameters.keySet()){
			vars+=key+"="+parameters.get(key)+", ";			
		}
		vars=vars.substring(0, vars.length()-2);
				
		return symbol+"("+vars+")";
	}
		
	@Override
	public int hashCode()
	{	
		//System.out.println("Hashcode: "+symbol+" -> "+symbol.hashCode());
		return symbol.hashCode();
	}
	
	@Override
	public boolean equals(Object obj)
	{
		return symbol.equals(((Symbol)obj).symbol);
	}
	
	public void setParameter(String name, float value){
		parameters.put(name, value);
	}
	
	public Float getParameter(String name){
		Float f = parameters.get(name);
		if(f==null){
			return SymbolFactory.getSymbolFactory().getDefaultParameter(name);
		}
		else
			return f;		
	}
	
	public static ArrayList<Symbol> createTestSymbols(){
		ArrayList<Symbol> symbols = new ArrayList<Symbol>();
		
		symbols.add(new Symbol("F"));
		symbols.add(new Symbol("B"));
		symbols.add(new Symbol("["));
		symbols.add(new Symbol("-"));
		symbols.add(new Symbol("F"));
		symbols.add(new Symbol("]"));
		symbols.add(new Symbol("["));
		symbols.add(new Symbol("+"));
		symbols.add(new Symbol("F"));
		symbols.add(new Symbol("]"));
		symbols.add(new Symbol("F"));
		
		return symbols;
	}

	public SymbolClass getSymbolClass()
	{
		return symbolClass;
	}

	public String getSymbol()
	{
		return symbol;
	}
	
}
